package huzh.foundation.dao;

import huzh.foundation.util.StringUtil;

import java.text.MessageFormat;
import java.util.Date;

/**
 *<p>title:SqlFormat</p>
 *<p>description:</p>
 * @since 2014-6-8
 * @author huzh
 * @version 1.0
 */
public class SqlFormat {
    /**msgFmt*/
    MessageFormat msgFmt = new MessageFormat("");

    /**DELIM_QUOTE*/
    public static char DELIM_QUOTE = '\'';

    /**DELIM_PERCENT*/
    public static char DELIM_PERCENT = '%';

    /**DELIM_UNDERBAR*/
    public static char DELIM_UNDERBAR = '_';

    /**DELIM_SEP*/
    public static char DELIM_SEP = '\\';

    /**REP_QUOTE*/
    public static String REP_QUOTE = "''";

    /**REP_PERCENT*/
    public static String REP_PERCENT = "\\%";

    /**REP_UNDERBAR*/
    public static String REP_UNDERBAR = "\\_";

    /**REP_SEP*/
    public static String REP_SEP = "\\\\";
    /**pattern*/
    String pattern;

    /**
     * @param newPattern
     */
    public SqlFormat(String newPattern) {
        this.pattern = newPattern;
    }

    /**
     * @param arguments
     * @param b
     * @return
     */
    public String format(String[] arguments, boolean b) {
        return format(this.pattern, arguments, b);
    }

    /**
     * @param s
     * @return
     */
    public static String analyseLikeString(String s) {
        char[] delim = { DELIM_QUOTE, DELIM_PERCENT, DELIM_UNDERBAR, DELIM_SEP };
        String[] reps = { REP_QUOTE, REP_PERCENT, REP_UNDERBAR, REP_SEP };
        return analyseString(s, delim, reps);
    }

    /**
     * @param c
     * @param delims
     * @return
     */
    private static int contains(char c, char[] delims) {
        for (int i = 0; i < delims.length; i++) {
            if (delims[i] == c) {
                return i;
            }
        }
        return -1;
    }

    /**
     * @param s
     * @param delims
     * @param reps
     * @return
     */
    private static String analyseString(String s, char[] delims, String[] reps){
        if ((s == null) || (delims == null) || (reps == null) || (delims.length != reps.length)){
            return null;
        }
        StringBuffer result = new StringBuffer();
        int k = 0; 
        int j = 0; 
        for (int i = 0; i < s.length(); j++) {
            char c = s.charAt(i);
            k = contains(c, delims);
            if (k >= 0) {
                result.append(reps[k]);
            } else
                result.append(c);
            i++;
        }

        return new String(result);
    }

    /**
     * @param s
     * @return
     */
    public static String analyseString(String s) {
        char[] delim = { DELIM_QUOTE };
        String[] reps = { REP_QUOTE };
        return analyseString(s, delim, reps);
    }

    /**
     * @param pattern
     * @param arguments
     * @param bConvert
     * @return
     */
    public static String format(String pattern, Object[] arguments, boolean bConvert){
        Object[] reps = null;
        if (pattern == null) {
            return pattern;
        }
        if (arguments != null) {
            reps = new Object[arguments.length];
            for (int i = 0; i < arguments.length; i++) {
                if (bConvert) {
                    reps[i] = analyseString(arguments[i].toString());
                }else {
                    reps[i] = arguments[i].toString();
                }
            }
        }

        pattern = StringUtil.replaceStr(pattern, "'", "''");
        return MessageFormat.format(pattern, reps);
    }

    /**
     * @param column
     * @param s
     * @return
     */
    public static String chkSQLString(String column, String s){
        return column + "=" + chkSQLString(s);
    }

    /**
     * @param s
     * @return
     */
    public static String chkSQLString(String s) {
        if (s == null) {
            return "''";
        }

        return "'" + s + "'";
    }

    /**
     * @param column
     * @param i
     * @return
     */
    public static String chkSQLInt(String column, int i) {
        return column + "=" + chkSQLInt(i);
    }

    /**
     * @param i
     * @return
     */
    public static String chkSQLInt(int i){
        String iStr = String.valueOf(i);
        if (iStr.equals("null")) {
            return "null";
        }

        return iStr;
    }

    /**
     * @param column
     * @param i
     * @return
     */
    public static String chkSQLLong(String column, long i){
        return column + "=" + chkSQLLong(i);
    }

    /**
     * @param i
     * @return
     */
    public static String chkSQLLong(long i){
        if (i == -1L) {
            return "null";
        }

        return String.valueOf(i);
    }

    /**
     * @param column
     * @param d
     * @return
     */
    public static String chkSQLDate(String column, Date d){
        return column + "=" + chkSQLDate(d);
    }

    /**
     * @param d
     * @return
     */
    public static String chkSQLDate(Date d){
        if (d == null) {
            return "null";
        }

        return "TO_DATE('" + d.toString() + "','YYYY-MM-DD')";
    }
}
